﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Print.Domain.DeliveryNumberGenerator
{
    class EMSDeliveryNumberGenerator : DeliveryNumberGenerator
    {
        int[] multy = new int[] { 8, 6, 4, 2, 3, 5, 9, 7 };

        public override bool AcceptDeliveryCompany(string arg)
        {
            return arg.Contains("EMS") && arg.Contains("经济");
        }

        public override string GenenrateNext(string deliveryNumberAlgorithm, string current)
        {
            if (string.IsNullOrWhiteSpace(current) || current.Length != 13)
            {
                throw new Exception("EMS快递问题格式不正确" + current);
            }

            string pre = current.Substring(0, 2);
            string last = current.Substring(11, 2);
            long number = long.Parse(current.Substring(2, 8)) + 1;
            int[] numbers = number.ToString("D8").Select(c => int.Parse(c.ToString())).ToArray();
            int total = 0;

            for (int i = 0; i < numbers.Length; i++)
            {
                total += multy[i] * numbers[i];
            }

            int res = 11 - total % 11;
            if (res == 10)
            {
                res = 0;
            }
            else if (res == 11)
            {
                res = 5;
            }
            else
            {

            }
            string newNumber = pre + number.ToString("D8") + res + last;
            return newNumber;
        }

        public override string GenenrateOnline(Order order, string wuliuId)
        {
            throw new NotImplementedException();
        }

        public override void CancelOnline(WuliuNumber wuliuNumber)
        {
            throw new NotImplementedException();
        }

        public override string GenenrateWuliuId(params string[] orderIds)
        {
            throw new NotImplementedException();
        }
    }
}
